--头文件
local skynet = require "skynet"
require "skynet.manager"
local cluster = require "skynet.cluster"
local log = require "sky_log"
local srv_config = require "srv_config"
local nn = require "sky_node"

local manager = {}

local node_name = skynet.getenv("node_name")
local port = tonumber(nn[node_name]) 

function manager:init()
    log.info("-----------------------game_manager init-----------------------")
    skynet.register("."..node_name)
    --集群模式开启节点名
    assert(port)
    if FRAME_CALL_TYPE==__CALL_TYPE_CLUSTER__ then
        cluster.open(node_name)
        log.warning("cluster open "..node_name.." port:"..(port-5000))
    end
    --log.debug("srv_config=", srv_config)
    --开启服务
    self:start_service()
end

function manager:start_service()
	--websocket
	local websocket_watchdog = skynet.uniqueservice("handle_websocket_watchdog")
	skynet.call(websocket_watchdog, "lua", "start", {
		port = port,
		maxclient = 64,
		nodelay = true,
	})

    --mysql
	self.mysql = skynet.uniqueservice("skynet_mysql_tool")
    --redis
	self.redis = skynet.uniqueservice("skynet_redis_tool")

    --config
    skynet.uniqueservice("service_config")

    --ssdb
	--skynet.uniqueservice("skynet_ssdb_tool")
	--websocket
	--skynet.uniqueservice("service_handle_msg")

    self.room_manager = skynet.uniqueservice("service_room_manager")
end

--start
skynet.start(function()
    skynet.dispatch("lua", function(session, address, cmd, ...)
        --log.info("manager dispatch cmd=", cmd)
        local f = manager[cmd]
        if f then
            skynet.ret(skynet.pack(f(manager, ...)))
        else
            error(string.format("Unknown command %s", tostring(cmd)))
        end
    end)
    
    skynet.register(".manager")
    manager:init()
end)